﻿Imports System.Data
Imports System.Data.SqlClient
Imports Entidad

Public Class AuditoriaDatos

    Sub actualizarUsuarioEstadoPedido(usuario As String, estado As String, cia As String, suc As String, tdoc As String, codpedido As String)
        Dim ds As New DataSet
        Dim conex As New Conexion
        Dim coneccion As New SqlConnection
        Try
            coneccion = conex.conectar()
            coneccion.Open()

            Dim query As String = "UPDATE Documento_Cliente " &
                                  "SET USU_Codigo = @USU, DCI_Estado = @EST, DCI_UltModificacion = GETDATE() " &
                                  "WHERE CIA = @CIA AND SUC_Codigo = @SUC " &
                                  "AND TDOC_COdigo = @TDOC AND DCI_Codigo = @DCI"

            Dim comando As New SqlCommand(query, coneccion)
            With comando
                .Parameters.AddWithValue("@CIA", cia)
                .Parameters.AddWithValue("@SUC", suc)
                .Parameters.AddWithValue("@TDOC", tdoc)
                .Parameters.AddWithValue("@DCI", codpedido)
                .Parameters.AddWithValue("@USU", usuario)
                .Parameters.AddWithValue("@EST", estado)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            adapter.Fill(ds)
        Catch ex As SqlException
            MsgBox("La conexión no está disponible.")
        Catch e As Exception
            MsgBox(e.Message)
        Finally
            coneccion.Close()
        End Try
    End Sub

    Function obtenerAsignador(codPedido As String) As DataSet
        Dim ds As New DataSet
        Dim conex As New Conexion
        Dim coneccion As New SqlConnection
        Try
            coneccion = conex.conectar()
            coneccion.Open()

            Dim query As String = "SELECT TOP 1 u.USU_Codigo,u.USU_Descripcion AS Asignador " &
                                  "FROM AuditoriaDocumentoCliente adc " &
                                  "INNER JOIN Usuario u " &
                                  "ON u.CIA = adc.CIA AND u.SUC_Codigo = adc.SUC_Codigo " &
                                  "AND u.USU_Codigo = adc.USU_Codigo AND u.USU_Activo = '1' " &
                                  "WHERE adc.DCI_Codigo = @DCI AND adc.EST_Codigo = '15'"

            Dim comando As New SqlCommand(query, coneccion)
            With comando
                .Parameters.AddWithValue("@DCI", codPedido)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            adapter.Fill(ds)
        Catch ex As SqlException
            MsgBox("La conexión no está disponible.")
        Catch e As Exception
            MsgBox(e.Message)
        Finally
            coneccion.Close()
        End Try

        Return ds
    End Function

End Class
